﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using QS.Logic.Objects;
using QS.Logic.Modules;
using QS.Game.Renderer;

namespace QS.Drawables
{
    class QEngineRenderer : QAbstractRenderer
    {
        private VertexBuffer m_VertexBuffer;
        private IndexBuffer m_IndexBuffer;
        private BasicEffect m_Effect;
        VertexPositionNormalTexture[] vertices;
        short[] indices;
        QRendererCollection s;
        public override void LoadContent(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {

            m_VertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionNormalTexture), 8, BufferUsage.WriteOnly);
            vertices = new VertexPositionNormalTexture[8];
            vertices[0] = new VertexPositionNormalTexture(new Vector3(0, 0, 0), Vector3.Forward, new Vector2(0,1));
            vertices[1] = new VertexPositionNormalTexture(new Vector3(1, 0, 0), Vector3.Forward, new Vector2(0, 0));
            vertices[2] = new VertexPositionNormalTexture(new Vector3(1, 0, 1), Vector3.Forward, new Vector2(1, 1));
            vertices[3] = new VertexPositionNormalTexture(new Vector3(0, 0, 1), Vector3.Forward, new Vector2(1, 0));
            vertices[4] = new VertexPositionNormalTexture(new Vector3(0, 1, 0), Vector3.Forward, new Vector2(1, 1));
            vertices[5] = new VertexPositionNormalTexture(new Vector3(1, 1, 0), Vector3.Forward, new Vector2(0, 0));
            vertices[6] = new VertexPositionNormalTexture(new Vector3(1, 1, 1), Vector3.Forward, new Vector2(1, 0));
            vertices[7] = new VertexPositionNormalTexture(new Vector3(0, 1, 1), Vector3.Forward, new Vector2(0, 1));
            m_VertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
            indices = new short[36];
            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 2; indices[4] = 3; indices[5] = 0;

            indices[6] = 4; indices[7] = 5; indices[8] = 6;
            indices[9] = 6; indices[10] = 7; indices[11] = 4;

            indices[12] = 0; indices[13] = 1; indices[14] = 5;
            indices[15] = 5; indices[16] = 4; indices[17] = 0;

            indices[18] = 2; indices[19] = 3; indices[20] = 7;
            indices[21] = 7; indices[22] = 6; indices[23] = 2;

            indices[24] = 1; indices[25] = 2; indices[26] = 6;
            indices[27] = 6; indices[28] = 5; indices[29] = 1;

            indices[30] = 0; indices[31] = 3; indices[32] = 7;
            indices[33] = 7; indices[34] = 4; indices[35] = 0;

            m_IndexBuffer = new IndexBuffer(graphicsDevice, typeof(short), 36, BufferUsage.WriteOnly);
            m_IndexBuffer.SetData(indices);
            m_Effect = new BasicEffect(graphicsDevice);
        }

        public override void Draw(GraphicsDevice device, QCamera camera, SpriteBatch spriteBatch, IDrawableObject drawableObject)
        {
            //QShipEngine engine = (QShipEngine)drawableObject;
            //device.SetVertexBuffer(m_VertexBuffer);
            //device.Indices = m_IndexBuffer;
            //m_Effect.World = Matrix.CreateTranslation(-engine.ArrayPosition.Y - 1, engine.ArrayPosition.Z, engine.ArrayPosition.X);
            //m_Effect.View = camera.ViewMatrix;
            //m_Effect.Projection = camera.ProjectionMatrix;
            //m_Effect.VertexColorEnabled = true;

            //RasterizerState rasterizerState = new RasterizerState();
            //rasterizerState.CullMode = CullMode.None;
            //device.RasterizerState = rasterizerState;

            //foreach (EffectPass pass in m_Effect.CurrentTechnique.Passes)
            //{
            //    pass.Apply();
            //    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
            //}
            QRendererCollection.MetalRenderer.Draw(vertices, indices,camera);
            
        }
    }
}
